function TabSheet(param) {

	var that = this;
	
	this.j_tabsheet = null;
	this.j_tab_content_cancel = null;
	
	var cur_tab = null;
	var count = 0;
	
	var url_rewrite = false;
	
	function __construct(p) {
	try{
		for(pkey in p) {
			if(pkey[0] != "_") {
				that[pkey] = p[pkey];
			}
		}
		if(p._url_rewrite) {
			url_rewrite = p._url_rewrite;
		}
		if(that.j_tab_content_cancel) { that.j_tab_content_cancel.click(cancelButtonClick); }
		
		initTabSheet();
		
		if(url_rewrite) {
			window.onpopstate = function(event) { 
			try{
  				//alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
				var historyState = event.state;
				if(historyState && historyState.index !== 'undefined') {
					document.title += historyState.index; 
  					simulateTabClick(historyState.index);
				}
				//else {
					//document.title += 3;
    				//simulateTabClick(0);
				//}
			}catch(err) {
				alert(err);
			}
			};
		}
		
		/*
		jq(window).bind('popstate', function() {  
		try{
			var historyState = history.state;
    		if(history && history.state && history.state.index !== 'undefined') {
    			simulateTabClick(historyState.index);
    		}else {
    			simulateTabClick(0);
    		}
    	}catch(err) {
			alert(err);
		}
		});
		*/
	}catch(err) {
		alert(err);
	}
	}
	
	function initTabSheet() {
		jq.each(that.j_tabsheet, function(index, val) {
			var j_tab_stuff = that.j_tabsheet[index];
			var j_tab = j_tab_stuff.j_tab;
			var j_tab_content = j_tab_stuff.j_tab_content;
			var j_tab_dependency = j_tab_stuff.j_tab_dependency;
			var j_tab_selected_by_default = j_tab_stuff.j_tab_selected_by_default;
			
			if(url_rewrite) {
				var j_tab_url = j_tab_stuff.j_tab_url;
				j_tab.data({"index" : index, "content" : j_tab_content, "dependency" : j_tab_dependency, "url" : j_tab_url});
			}else {
				j_tab.data({"index" : index, "content" : j_tab_content, "dependency" : j_tab_dependency});
			}
			
			if(j_tab_selected_by_default === true) {
				cur_tab = j_tab;
    			showSelectedTab(j_tab);
    			// replace URL
				if(url_rewrite) {
					replaceStates({index: cur_tab.data("index")}, document.title, j_tab_url);
				}
			}
			
			j_tab.click(tabClick);
			count++;
		});
		
	}
	
	function simulateTabClick(index) {
	try{
		if(index == "none") {
			document.title += "none";
			//hide current tab
			//hideCurrentTab();
			//cur_tab = null;
			cancelButtonClick();
			return;
		}
		var selected_tab = that.j_tabsheet[index].j_tab;
	
		var selected_tab_dependency = selected_tab.data("dependency");
		if(cur_tab == null) {
			showSelectedTab(selected_tab);
    		cur_tab = selected_tab_dependency;
		}else {
			if(selected_tab_dependency.data("index") === cur_tab.data("dependency").data("index")) {
				//do nothing
			}else {
				//hide current tab
				hideCurrentTab();
				//show select tab
				showSelectedTab(selected_tab);
    			cur_tab = selected_tab_dependency;
			}
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function tabClick(e) {
	try{
		e.preventDefault();
		
		var selected_tab = jq(this);
		
		var selected_tab_dependency = selected_tab.data("dependency");
		if(cur_tab == null) {
			showSelectedTab(selected_tab);
    		cur_tab = selected_tab_dependency;
    		// update URL
			if(url_rewrite) {
				document.title += cur_tab.data("index");
				changeStates({index: cur_tab.data("index")}, document.title, selected_tab.data("url"));
			}
		}else {
			if(selected_tab_dependency.data("index") === cur_tab.data("dependency").data("index")) {
				//do nothing
			}else {
				//hide current tab
				hideCurrentTab();
				//show select tab
				showSelectedTab(selected_tab);
    			cur_tab = selected_tab_dependency;
    			
				// update URL
				if(url_rewrite) {
					document.title += cur_tab.data("index");
					changeStates({index: cur_tab.data("index")}, document.title, selected_tab.data("url"));
				}
			}
		}
	}catch(err) {
		alert(err);
	}
	}
	
	function showSelectedTab(selected_tab) {
	try{
		var selected_tab_content = selected_tab.data("content");
		var selected_tab_dependency = selected_tab.data("dependency");
		
    	if(!selected_tab_dependency.hasClass("active")) {
			selected_tab_dependency.addClass("active");
    	}
    	
		if(!selected_tab_content.hasClass("active")) {
			selected_tab_content.addClass("active");
    	}
    }catch(err) {
		alert(err);
	}
	}
	
	function hideCurrentTab() {
	try{
		if(cur_tab != null) {
			var cur_tab_content = cur_tab.data("content");
			if(cur_tab.hasClass("active")) {
				cur_tab.removeClass("active");
    		}
    		if(cur_tab_content.hasClass("active")) {
				cur_tab_content.removeClass("active");
    		}
		}
    }catch(err) {
		alert(err);
	}
	}
	
    function changeStates(stateObject, title, url) {
    try{
		if (history && history.pushState) {
    		history.pushState(stateObject, title, url);
    	}
	}catch(err) {
		alert(err);
	}
    }
    
    function replaceStates(stateObject, title, url) {
    try{
		if (history && history.replaceState) {
    		history.replaceState(stateObject, title, url);
    	}
	}catch(err) {
		alert(err);
	}
    }
    
    function cancelButtonClick() {
	try{
	
		hideCurrentTab();
		cur_tab = null;
	
	}catch(err) {
		alert(err);
	}
	}
	
	__construct(param);
	
}

